package leetcode.editor.vscdev.greedy;

import java.util.Arrays;

public class lc153 {

}

class Solution {
    public int candy(int[] ratings) {
        return -1;
    }

    public int func1(int[] ratings) {
        int[] candy = new int[ratings.length];
        candy[0] = 1;
        // 从左到右遍历
        for (int i = 1; i < ratings.length; i++) {
            candy[i] = 1;
            if (ratings[i] > ratings[i - 1]) {// 右大于左边
                candy[i] = candy[i - 1] + 1;
            }
        }
        // 从右到左遍历
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {// 左大于右
                candy[i] = Math.max(candy[i], candy[i + 1] + 1);// 取左右比较的最大值, 确保最大
            }
        }
        return Arrays.stream(candy).sum();
    }
}